package com.wms.wms.controller;


import com.wms.wms.entity.Power;
import com.wms.wms.entity.User;
import com.wms.wms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping(value = {"/admin"})
@Controller
public class UserController {
    @Autowired
    UserService userService;
    //   UserInfo userInfo;
    User user;
    Power power;


    /**
     * 跳转到登陆测试页面
     *
     * @return 登陆页面
     */
    @RequestMapping(value = {"/"})
    public String registerPage() {

        if(user!=null) {
            user = null;
        }
          return "personInfo/login";
    }

    /**
     * 用户登录
     */
    @RequestMapping(value = {"user/login"})
    //  @ResponseBody
    public String login(String adminId, String password, Model model, HttpServletRequest request) {
        System.out.println(adminId);
        System.out.println(password);
        // userInfo = userService.login(adminId,password);
        user = userService.login(adminId, password);
        if (user == null) {
            //TODO异常处理后续完成
            return "login";
        } else {
            System.out.println("登录用户信息：" + user.toString());
        }
        model.addAttribute("currentName", user.getAdminName());
        HttpSession session = request.getSession();
        if (user.getRole() == 1) {
            power=new Power();
            power.setManage("display: none;");
            power.setProduct("display: none;");
            session.setAttribute("role",1);
        } else if (user.getRole() == 2) {
            power=new Power();
            power.setManage("display: none;");
            power.setProduct("");
            session.setAttribute("role",2);
        } else if (user.getRole() == 3) {
            power=new Power();
            power.setManage("");
            power.setProduct("");
            session.setAttribute("role",3);
        }
        model.addAttribute("manage", power.getManage());
        model.addAttribute("product",power.getProduct());
        return "personInfo/dashboard";
    }

    @RequestMapping(value = {"user/adminPage"})
    public String adminPage(Model model) {
        model.addAttribute("currentName", user.getAdminName());
        model.addAttribute("manage", power.getManage());
        model.addAttribute("product",power.getProduct());
        return "personInfo/dashboard";
    }



    @RequestMapping(value = {"user/proPage"})
    public String proPage(Model model) {
        model.addAttribute("adminId", user.getAdminId());
        model.addAttribute("adminName", user.getAdminName());
        if (user.getRole() == 1) {
            model.addAttribute("role", "admin");
        } else if (user.getRole() == 2) {
            model.addAttribute("role", "manager");
        } else if (user.getRole() == 3) {
            model.addAttribute("role", "HR");
        }
        model.addAttribute("password", user.getPassword());
        model.addAttribute("manage", power.getManage());
        model.addAttribute("product",power.getProduct());
        return "personInfo/profile";
    }

    @RequestMapping(value = {"user/editPro"})
    public String editPro(String password, Model model) {
        user.setPassword(password);
        userService.edit(user);
        // user.setPassword(pass);
        model.addAttribute("adminId", user.getAdminId());
        model.addAttribute("adminName", user.getAdminName());
        if (user.getRole() == 1) {
            model.addAttribute("role", "admin");
        } else if (user.getRole() == 2) {
            model.addAttribute("role", "manager");
        } else if (user.getRole() == 3) {
            model.addAttribute("role", "HR");
        }
        model.addAttribute("password", user.getPassword());
        model.addAttribute("manage", power.getManage());
        model.addAttribute("product",power.getProduct());
        return "personInfo/profile";
    }

    @RequestMapping(value = {"user/tablePage"})
    public String tablePage(Model model) {
        List<User> list = userService.userList();
        for (User u : list) {
            System.out.println(u.getAdminName());
        }
        model.addAttribute("adminName", user.getAdminName());
        model.addAttribute("us", list);
        model.addAttribute("manage", power.getManage());
        model.addAttribute("product",power.getProduct());
        return "personInfo/table";
    }


    @RequestMapping(value = {"user/deleteUser"})
    public String deleteUser(String adminId, Model model) {
        System.out.println(adminId);

        userService.deleteUser(adminId);
        List<User> list = userService.userList();
        for (User u : list) {
            System.out.println(u.getAdminName());
        }
        model.addAttribute("adminName", user.getAdminName());
        model.addAttribute("us", list);
        model.addAttribute("manage", power.getManage());
        model.addAttribute("product",power.getProduct());
        return "personInfo/table";
    }

    @RequestMapping(value = {"user/editUser"})
    public String editUser(String adminId, Model model) {
            User user1 = userService.selectUser(adminId);
            model.addAttribute("adminId", user1.getAdminId());
            model.addAttribute("adminName", user1.getAdminName());
            if (user1.getRole() == 1) {
                model.addAttribute("role", "admin");
            } else if (user1.getRole() == 2) {
                model.addAttribute("role", "manager");
            } else if (user1.getRole() == 3) {
                model.addAttribute("role", "HR");
            }
            model.addAttribute("password", user1.getPassword());
            model.addAttribute("currentName", user.getAdminName());
        model.addAttribute("manage", power.getManage());
        model.addAttribute("product",power.getProduct());
            return "personInfo/editUser";
    }

    @RequestMapping(value = {"user/editaction"})
    public String editaction(String adminId, String adminName, String role, String password, Model model) {
            User user2 = new User();
            user2.setAdminId(adminId);
            user2.setAdminName(adminName);
            if (role.equals("admin")) {
                user2.setRole(1);
            } else if (role.equals("manager")) {
                user2.setRole(2);
            } else if (role.equals("HR")) {
                user2.setRole(3);
            }
            user2.setPassword(password);
            userService.editUser(user2);
            model.addAttribute("adminId", user2.getAdminId());
            model.addAttribute("adminName", user2.getAdminName());
            if (user2.getRole() == 1) {
                model.addAttribute("role", "admin");
            } else if (user2.getRole() == 2) {
                model.addAttribute("role", "manager");
            } else if (user2.getRole() == 3) {
                model.addAttribute("role", "HR");
            }
            model.addAttribute("currentName", user.getAdminName());
            model.addAttribute("password", user2.getPassword());
        model.addAttribute("manage", power.getManage());
        model.addAttribute("product",power.getProduct());
           return "personInfo/editUser";
    }

    @RequestMapping(value = {"user/insertUser"})
    public String insertUser(String adminId,Model model) {
        if(adminId==null){
            model.addAttribute("adminId", "");
            model.addAttribute("adminName", "");
            model.addAttribute("role", "");
            model.addAttribute("password", "");
        }else {
            User user1 = userService.selectUser(adminId);
            model.addAttribute("adminId", user1.getAdminId());
            model.addAttribute("adminName", user1.getAdminName());
            if (user1.getRole() == 1) {
                model.addAttribute("role", "admin");
            } else if (user1.getRole() == 2) {
                model.addAttribute("role", "manager");
            } else if (user1.getRole() == 3) {
                model.addAttribute("role", "HR");
            }
            model.addAttribute("password", user1.getPassword());
        }

        model.addAttribute("currentName", user.getAdminName());
        model.addAttribute("manage", power.getManage());
        model.addAttribute("product",power.getProduct());
        return "personInfo/insertUser";
    }

    @RequestMapping(value = {"user/insertaction"})
    public String insertaction(String adminId, String adminName, String role, String password, Model model) {
        User user2 = new User();
        user2.setAdminId(adminId);
        user2.setAdminName(adminName);
        if (role.equals("admin")) {
            user2.setRole(1);
        } else if (role.equals("manager")) {
            user2.setRole(2);
        } else if (role.equals("HR")) {
            user2.setRole(3);
        }
        user2.setPassword(password);
        userService.insertUser(user2);
        model.addAttribute("adminId", user2.getAdminId());
        model.addAttribute("adminName", user2.getAdminName());
        if (user2.getRole() == 1) {
            model.addAttribute("role", "admin");
        } else if (user2.getRole() == 2) {
            model.addAttribute("role", "manager");
        } else if (user2.getRole() == 3) {
            model.addAttribute("role", "HR");
        }
        model.addAttribute("currentName", user.getAdminName());
        model.addAttribute("password", user2.getPassword());
        model.addAttribute("manage", power.getManage());
        model.addAttribute("product",power.getProduct());
        return "personInfo/insertUser";
    }


}
